package com.blvtphan.tipocketclubs.adapter;

import java.util.List;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Build;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.blvtphan.tipocketclubs.R;
import com.blvtphan.tipocketclubs.custom.CustomTextView;
import com.blvtphan.tipocketclubs.gui.MainActivity;
import com.blvtphan.tipocketclubs.model.CacheBitmap;
import com.blvtphan.tipocketclubs.model.PlaceDetailModel;
import com.blvtphan.tipocketclubs.util.GeneralUtils;

public class PlaceDetailListItemAdapter extends BaseAdapter {

	private Context mContext;

	private List<PlaceDetailModel> mLstPlacesModel;

	public PlaceDetailListItemAdapter(Context context,
			List<PlaceDetailModel> mLstPlacesModel) {
		this.mContext = context;
		this.mLstPlacesModel = mLstPlacesModel;
	}

	@SuppressLint("NewApi")
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		LayoutInflater inflater = (LayoutInflater) mContext
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		View rowView = inflater.inflate(R.layout.item_place_list_details,
				parent, false);

		CustomTextView tvPlaceName = (CustomTextView) rowView
				.findViewById(R.id.txt_place_list_name);
		CustomTextView tvPlaceType = (CustomTextView) rowView
				.findViewById(R.id.txt_place_list_type);
		CustomTextView tvPlaceTip = (CustomTextView) rowView
				.findViewById(R.id.txt_place_list_tip);
		CustomTextView tvPlaceAddress = (CustomTextView) rowView
				.findViewById(R.id.txt_place_list_address);
		CustomTextView tvPlaceDiscount = (CustomTextView) rowView
				.findViewById(R.id.txt_place_list_discount);
		final ImageView imgPlaceLogo = (ImageView) rowView
				.findViewById(R.id.img_place_list_logo);

		tvPlaceName.setText(mLstPlacesModel.get(position).getName());
		tvPlaceType.setText(mLstPlacesModel.get(position).getCategory());
		tvPlaceTip.setText("For sale");
		tvPlaceAddress.setText(mLstPlacesModel.get(position).getAddress());
		tvPlaceDiscount.setText(mLstPlacesModel.get(position).getLstRewards()
				.get(0).getDiscount());

		Bitmap bitmap = MainActivity.mCache
				.getBitmapFromMemCache((mLstPlacesModel.get(position)
						.getMarchantLogo()));
		imgPlaceLogo.setImageBitmap(bitmap);

		AsyncTask<Void, Void, CacheBitmap> task = new AsyncTask<Void, Void, CacheBitmap>() {

			@Override
			protected CacheBitmap doInBackground(Void... params) {

				Bitmap tmp = MainActivity.mCache
						.getBitmapFromMemCache(mLstPlacesModel.get(position)
								.getMarchantLogo());
				if (tmp != null) {
					CacheBitmap bt = new CacheBitmap();
					bt.setKey(mLstPlacesModel.get(position).getMarchantLogo());
					bt.setBitmap(tmp);
					Log.d("Using Image Memory Cache :", "FYI");
					return bt;
				} else {
					Log.d("Downloading Images :", "FYI");
					return GeneralUtils.downloadImageWithCache(mContext, mLstPlacesModel
							.get(position).getMarchantLogo());
				}
			}

			@Override
			protected void onPostExecute(CacheBitmap result) {
				if (result != null) {
					MainActivity.mCache.addBitmapToMemoryCache(result.getKey(),
							result.getBitmap());
					imgPlaceLogo.setImageBitmap(result.getBitmap());
				}
			}
		};

		if (Build.VERSION.SDK_INT >= 11) {
			task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
		} else {
			task.execute();
		}
		return rowView;

	}

	@Override
	public int getCount() {
		return mLstPlacesModel.size();
	}

	@Override
	public Object getItem(int arg0) {
		return mLstPlacesModel.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}
}
